package com.duoec.magichour.service.impl;

import com.duoec.magichour.dto.UserInfo;
import com.duoec.magichour.dto.response.UserSimpleInfoDto;
import com.duoec.magichour.dto.response.UserWithFollowDto;
import com.duoec.magichour.mapper.UserFollowMapper;
import com.duoec.magichour.pojo.UserFollow;
import com.duoec.magichour.service.UserFollowService;
import com.duoec.magichour.service.UserService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ycoe on 17/6/27.
 */
@Service
public class UserFollowServiceImpl implements UserFollowService {
    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserService userService;

    @Override
    public boolean isFollow(long userId, long followUserId) {
        String id = userId + "_" + followUserId;
        return userFollowMapper.exists(id);
    }

    @Override
    public List<UserWithFollowDto> getFollowedUserList(UserInfo userInfo, long userId, int pageNo, int pageSize) {
        List<UserFollow> userFollows = userFollowMapper.getFollowedUserList(userId, (pageNo - 1) * pageSize, pageSize);
        return getUserWithFollowDtos(userInfo, userId, userFollows);
    }

    @Override
    public List<UserWithFollowDto> getFansList(UserInfo userInfo, long userId, int pageNo, int pageSize) {
        List<UserFollow> userFollows = userFollowMapper.getFansList(userId, (pageNo - 1) * pageSize, pageSize);
        return getUserWithFollowDtos(userInfo, userId, userFollows);
    }

    private List<UserWithFollowDto> getUserWithFollowDtos(UserInfo userInfo, long userId, List<UserFollow> userFollows) {
        List<UserWithFollowDto> list = Lists.newArrayList();
        if (userFollows == null || userFollows.isEmpty()) {
            return list;
        }
        long myUserId = userInfo.getId();
        List<Long> userIds = Lists.newArrayList();
        boolean isMyself = userInfo.getId().equals(userId);
        userFollows.forEach(userFollow -> {
            Long followUserId = userFollow.getFollowUserId();
            userIds.add(followUserId);
        });

        //获取用户基本信息
        Map<Long, UserSimpleInfoDto> userMap = userService.getUserSimpleInfoMap(userIds);

        Set<Long> followedUserId = null;
        if (!isMyself) {
            //如果当前用户与userId不同时，需要检查哪些用户已关注过
            followedUserId = userFollowMapper.getFollowedUsers(myUserId, userIds);
        }
        for (UserFollow userFollow : userFollows) {
            UserWithFollowDto userWithFollow = new UserWithFollowDto();
            UserSimpleInfoDto userSimpleInfoDto = userMap.get(userFollow.getFollowUserId());
            if (userSimpleInfoDto == null) {
                continue;
            }
            BeanUtils.copyProperties(userSimpleInfoDto, userWithFollow);
            if (isMyself) {
                userWithFollow.setFollow(true);
            } else if (followedUserId != null) {
                userWithFollow.setFollow(followedUserId.contains(userFollow.getFollowUserId()));
            }
            list.add(userWithFollow);
        }

        return list;
    }

    @Transactional
    @Override
    public boolean follow(long userId, long followUserId) {
        String id = userId + "_" + followUserId;
        if (userFollowMapper.exists(id)) {
            //已经关注过
            return true;
        }

        UserFollow userFollow = new UserFollow();
        userFollow.setId(id);
        userFollow.setUserId(userId);
        userFollow.setFollowUserId(followUserId);
        userFollowMapper.insertSelective(userFollow);

        //用户关注数+1
        userService.incFavoriteUserCount(userId);

        //被关注用户被关注数+1
        userService.incFansCount(followUserId);

        return userFollow != null;
    }
}
